package com.jaydenxiao.common.commonutils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.jaydenxiao.common.R;


public class DialogUtils {
    public static Dialog createInfoDialog(Context context, String info) {
        LayoutInflater inflaterDl = LayoutInflater.from(context);
        View relativeLayout = inflaterDl.inflate(R.layout.layout_dialog_info, null);
        TextView titleTv = (TextView) relativeLayout.findViewById(R.id.title_tv);
        if (!TextUtils.isEmpty(info))
            titleTv.setText(info);
        final Dialog dialog = new Dialog(context, R.style.mDialogStyle_black);
        dialog.setCancelable(false);
//        dialog.setCanceledOnTouchOutside(false);
        int width = context.getResources().getDisplayMetrics().widthPixels;
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (width * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(relativeLayout, params);
        TextView confirmBt = (TextView) relativeLayout.findViewById(R.id.confirm_bt);
        confirmBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }

    public static Dialog createFuMiDialog(Context context, String title, String content) {
        LayoutInflater inflaterDl = LayoutInflater.from(context);
        View relativeLayout = inflaterDl.inflate(R.layout.dialog_fumi_failed, null);
        final Dialog dialog = new Dialog(context, R.style.mDialogStyle_black);
        TextView titleTv = (TextView) relativeLayout.findViewById(R.id.title_tv);
        TextView tv_content = (TextView) relativeLayout.findViewById(R.id.tv_content);
        titleTv.setText(title);
        tv_content.setText(content);
        dialog.setCancelable(false);
//        dialog.setCanceledOnTouchOutside(false);
        int width = context.getResources().getDisplayMetrics().widthPixels;
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (width * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(relativeLayout, params);
        TextView confirmBt = (TextView) relativeLayout.findViewById(R.id.confirm_bt);
        confirmBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }

    public static Dialog createInfoDialogWithListener(Context context, String info, final OnComfirmListening listener) {
        LayoutInflater inflaterDl = LayoutInflater.from(context);
        View relativeLayout = inflaterDl.inflate(R.layout.layout_dialog_info, null);
        TextView titleTv = (TextView) relativeLayout.findViewById(R.id.title_tv);
        if (!TextUtils.isEmpty(info))
            titleTv.setText(info);
        final Dialog dialog = new Dialog(context, R.style.mDialogStyle_black);
        dialog.setCancelable(false);
//        dialog.setCanceledOnTouchOutside(false);
        int width = context.getResources().getDisplayMetrics().widthPixels;
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (width * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(relativeLayout, params);
        TextView confirmBt = (TextView) relativeLayout.findViewById(R.id.confirm_bt);
        confirmBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (listener != null) {
                    listener.confirm();
                }
            }
        });
        dialog.show();
        return dialog;
    }

    public static Dialog create2BtnDialogWithListener(Context context, String info, final OnComfirmListening listener) {
        LayoutInflater inflaterDl = LayoutInflater.from(context);
        View relativeLayout = inflaterDl.inflate(R.layout.layout_dialog_info_2, null);
        TextView titleTv = (TextView) relativeLayout.findViewById(R.id.title_tv);
        titleTv.setText(info);
        final Dialog dialog = new Dialog(context, R.style.mDialogStyle_black);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        int width = context.getResources().getDisplayMetrics().widthPixels;
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (width * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(relativeLayout, params);
        relativeLayout.findViewById(R.id.cancel_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        TextView confirmBt = (TextView) relativeLayout.findViewById(R.id.confirm_bt);
        confirmBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (listener != null) {
                    listener.confirm();
                }
            }
        });
        dialog.show();
        return dialog;
    }
    public static Dialog create2DialogWithListener(Context context,final OnComfirmListening listener) {
        LayoutInflater inflaterDl = LayoutInflater.from(context);
        View relativeLayout = inflaterDl.inflate(R.layout.layout_dialog_2, null);
        final Dialog dialog = new Dialog(context, R.style.mDialogStyle_black);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        int width = context.getResources().getDisplayMetrics().widthPixels;
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (width * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(relativeLayout, params);
        relativeLayout.findViewById(R.id.cancel_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        TextView confirmBt = (TextView) relativeLayout.findViewById(R.id.confirm_bt);
        confirmBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (listener != null) {
                    listener.confirm();
                }
            }
        });
        dialog.show();
        return dialog;
    }

    public static Dialog create2TuikuanBtnDialogWithListener(Context context, final OnComfirmListening listener) {
        LayoutInflater inflaterDl = LayoutInflater.from(context);
        View relativeLayout = inflaterDl.inflate(R.layout.layout_dialog_info_tuikuan, null);
        final Dialog dialog = new Dialog(context, R.style.mDialogStyle_black);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        int width = context.getResources().getDisplayMetrics().widthPixels;
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (width * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(relativeLayout, params);
        relativeLayout.findViewById(R.id.cancel_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        TextView confirmBt = (TextView) relativeLayout.findViewById(R.id.confirm_bt);
        confirmBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (listener != null) {
                    listener.confirm();
                }
            }
        });
        dialog.show();
        return dialog;
    }

    public static Dialog createOutRangeDialogWithListener(Context context, String title, String con1, String con2, String con3, String confim, String cancel, final OnComfirmListening listener) {
        LayoutInflater inflaterDl = LayoutInflater.from(context);
        View relativeLayout = inflaterDl.inflate(R.layout.dialog_out_range, null);
        TextView titletv = (TextView) relativeLayout.findViewById(R.id.title_tv);
        TextView content1 = (TextView) relativeLayout.findViewById(R.id.content_tv);
        TextView content2 = (TextView) relativeLayout.findViewById(R.id.content_tv2);
        TextView content3 = (TextView) relativeLayout.findViewById(R.id.content_tv3);
        TextView confirmBt = (TextView) relativeLayout.findViewById(R.id.cancel_bt);
        TextView cancelBt = (TextView) relativeLayout.findViewById(R.id.confirm_bt);

        if (!TextUtils.isEmpty(title)) {
            titletv.setText(title);
        }
        if (!TextUtils.isEmpty(confim)) {
            confirmBt.setText(confim);
        }
        if (!TextUtils.isEmpty(cancel)) {
            cancelBt.setText(cancel);
        }
        if (!TextUtils.isEmpty(con1)) {
            content1.setText(con1);
        } else {
            content1.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(con2)) {
            content2.setText(con2);
        } else {
            content2.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(con3)) {
            content3.setText(con3);
        } else {
            content3.setVisibility(View.GONE);
        }
        final Dialog dialog = new Dialog(context, R.style.mDialogStyle_black);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        int width = context.getResources().getDisplayMetrics().widthPixels;
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (width * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(relativeLayout, params);
        cancelBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        confirmBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (listener != null) {
                    listener.confirm();
                }
            }
        });
        dialog.show();
        return dialog;
    }

    public static Dialog createCustomDialoListener(Context context, String info, String left, String right, final OnComfirmListening listener, final DialogInterface.OnCancelListener oncancel) {
        LayoutInflater inflaterDl = LayoutInflater.from(context);
        View relativeLayout = inflaterDl.inflate(R.layout.layout_dialog_info_3, null);
        TextView titleTv = (TextView) relativeLayout.findViewById(R.id.title_tv);
        TextView cancelBt = (TextView) relativeLayout.findViewById(R.id.cancel_bt);
        TextView confirmBt = (TextView) relativeLayout.findViewById(R.id.confirm_bt);
        titleTv.setText(info);
        cancelBt.setText(left);
        confirmBt.setText(right);
        final Dialog dialog = new Dialog(context, R.style.mDialogStyle_black);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        int width = context.getResources().getDisplayMetrics().widthPixels;
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (width * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(relativeLayout, params);
        relativeLayout.findViewById(R.id.cancel_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        cancelBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (oncancel != null) {
                    oncancel.onCancel(dialog);
                }
            }
        });
        confirmBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (listener != null) {
                    listener.confirm();
                }
            }
        });
        dialog.show();
        return dialog;
    }

    public interface OnComfirmListening {
        void confirm();
    }

    public static Dialog createLoadingDialog(Context context, String info, boolean cancleable) {
        LayoutInflater inflaterDl = LayoutInflater.from(context);
        View relativeLayout = inflaterDl.inflate(R.layout.layout_dialog_loading, null);
        TextView titleTv = (TextView) relativeLayout.findViewById(R.id.tv_msg_load);
        if (!TextUtils.isEmpty(info))
            titleTv.setText(info);
//            ImageView imageView= (ImageView)relativeLayout.findViewById(R.id.iv_load);
//            imageView.startAnimation(AnimationUtils.loadAnimation(context,R.anim.rotating));
        final Dialog dialog = new Dialog(context, R.style.mDialogStyle_trans);
        dialog.setCancelable(cancleable);
//        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(relativeLayout);
        dialog.show();
        return dialog;
    }

    public static Dialog createLoadingDialogForHttp(Context context, String info, boolean cancleable, final DialogInterface.OnCancelListener onCancelListener) {

        LayoutInflater inflaterDl = LayoutInflater.from(context);
        View relativeLayout = inflaterDl.inflate(R.layout.layout_dialog_loading, null);
        TextView titleTv = (TextView) relativeLayout.findViewById(R.id.tv_msg_load);
        if (!TextUtils.isEmpty(info))
            titleTv.setText(info);
//            ImageView imageView= (ImageView)relativeLayout.findViewById(R.id.iv_load);
//            imageView.startAnimation(AnimationUtils.loadAnimation(context,R.anim.rotating));
        final Dialog dialog = new Dialog(context, R.style.mDialogStyle_trans);
        dialog.setCancelable(cancleable);
        dialog.setCanceledOnTouchOutside(false);
        if (cancleable)
            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    onCancelListener.onCancel(dialog);
                }
            });
        dialog.setContentView(relativeLayout);
        dialog.show();
        CountDownTimer timer = new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                dialog.setCancelable(true);
                dialog.dismiss();
            }
        };
        timer.start();
        return dialog;
    }

    public static void dismiss(Dialog dialog) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }

    //当抓完娃娃时弹出的弹框
    public static Dialog ShowBankDialog(final Activity context, final View.OnClickListener listener, final OnComfirmListening onComfirmListening) {
        final AlertDialog alertDialog = new android.app.AlertDialog.Builder(context, R.style.mDialogStyle_black).create();
        alertDialog.show();
        alertDialog.setCanceledOnTouchOutside(true);
        alertDialog.getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.dialog_bank_card);
        window.setGravity(Gravity.CENTER);  //此处可以设置dialog显示的位置
//        window.setWindowAnimations(R.style.AnimBottom);  //添加动画
        TextView tv_jieji = (TextView) window.findViewById(R.id.tv_jieji);
        TextView tv_xinyong = (TextView) window.findViewById(R.id.tv_xinyong);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = lp.MATCH_PARENT;
        lp.height = lp.WRAP_CONTENT;
        window.setAttributes(lp);

        tv_jieji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
                if (listener != null) {
                    listener.onClick(view);
                }
            }
        });
        tv_xinyong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
                if (onComfirmListening != null) {
                    onComfirmListening.confirm();
                }
            }
        });

        return alertDialog;
    }
}
